package com.example.hospital.mapper;

import com.example.hospital.domain.entity.ExaminationOrder;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ExaminationOrderMapper {
    @Insert("INSERT INTO examination_order (user_id, doctor_id, package_id, indicator_id, order_type, order_date, status, remark, create_time) " +
            "VALUES (#{userId}, #{doctorId}, #{packageId}, #{indicatorId}, #{orderType}, #{orderDate}, #{status}, #{remark}, NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "orderId")
    int insert(ExaminationOrder order);

    @Update("UPDATE examination_order SET status=#{status}, remark=#{remark} WHERE order_id=#{orderId}")
    int updateStatus(@Param("orderId") Long orderId, @Param("status") Integer status, @Param("remark") String remark);

    @Select("SELECT * FROM examination_order WHERE order_id=#{orderId}")
    ExaminationOrder findById(@Param("orderId") Long orderId);

    @Select({
            "<script>",
            "SELECT * FROM examination_order",
            "<where>",
            "  <if test=\"userId != null\"> AND user_id = #{userId} </if>",
            "  <if test=\"doctorId != null\"> AND doctor_id = #{doctorId} </if>",
            "  <if test=\"status != null\"> AND status = #{status} </if>",
            "  <if test=\"orderType != null and orderType != ''\"> AND order_type = #{orderType} </if>",
            "</where>",
            "ORDER BY create_time DESC",
            "LIMIT #{limit} OFFSET #{offset}",
            "</script>"
    })
    List<ExaminationOrder> list(@Param("userId") Long userId,
                                @Param("doctorId") Long doctorId,
                                @Param("status") Integer status,
                                @Param("orderType") String orderType,
                                @Param("limit") int limit,
                                @Param("offset") int offset);

    @Select({
            "<script>",
            "SELECT COUNT(1) FROM examination_order",
            "<where>",
            "  <if test=\"userId != null\"> AND user_id = #{userId} </if>",
            "  <if test=\"doctorId != null\"> AND doctor_id = #{doctorId} </if>",
            "  <if test=\"status != null\"> AND status = #{status} </if>",
            "  <if test=\"orderType != null and orderType != ''\"> AND order_type = #{orderType} </if>",
            "</where>",
            "</script>"
    })
    long count(@Param("userId") Long userId,
               @Param("doctorId") Long doctorId,
               @Param("status") Integer status,
               @Param("orderType") String orderType);
}